package module::stylebop_au::parser;
use Encode;
use strict;

# ---------------------------------------------
# constructor
# Modify Log
# 3/12/2013 Louise create
# ---------------------------------------------
sub new {
	my $class = shift;
	my $self  = {};
	bless $self, $class;
	return $self;
}
# ---------------------------------------------
# parse one product detail page
# Modify Log
# 3/12/2013 Louise create
# ---------------------------------------------
sub parseAndgetInfo {
	my $this = shift;
	my $url  = shift;
	my $charset = shift;
	my $hostUrl = shift;
	my $splitString = '<Split>';
	my $content = $main::filedata->getPage($url, $charset);
	
	if ( !$content ) {
		# E300001 : Failed to get detail information
		Log::Write::setLog( "E300001", { 'URL' => $url, 'POINT' => 'noContent' } );
	    return;
	}else{
		$content =~ s/<!--.*?-->//isg;
	}
	
	# Defind properties about product
	
	# get Description
	my $description = '';
	my @desArray = ();
	if ($content =~ /productInfo.*?productlisting><img(.*?)<\/div>/is){
		my $tempText = $1;
		while ($tempText =~ /productlisting>([^<]*)/isg){
			my $tempdesc = $1;
			push @desArray, $tempdesc;
		}
		$description = join "\n", @desArray;
		$description =~ s/&amp;/&/isg;
	}
	if (!$description){
    	# E300002 : Failed get ImageURL
    	Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'Description' } );
    }
	
	# get ImageUrl
	my $imageUrl = '';
	if ($content =~ /alt=["']Viewer["'].*?src=["']([^"]*)/is){
		$imageUrl = $1;
		if ($imageUrl !~ /^http/is){
			$imageUrl = $hostUrl.$imageUrl;
		}
	}elsif ($content =~ /<img\s*src=["']([^"']*)["'][^>]*product/is){
		$imageUrl = $1;
		if ($imageUrl !~ /^http/is){
			$imageUrl = $hostUrl.$imageUrl;
		}
	}
	if (!$imageUrl){
    	# E300002 : Failed get ImageURL
    	Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'ImageURL' } );
    	return;
    }
	
	# get ProductId
	my $productId = '';
	if ($content =~ /name=["']pid["']\s*value=["']([\d]+)["']/is){
		$productId = $1;
	}
	
	# get Size
	my @sizeArray = ();
	if ($content =~ /Available\s*Sizes[^<]*<\/option>(.*?)<\/select>/is){
		my $sizeText = $1;
		while ($sizeText =~ /<option[^>]*>([^<]*)<\/option>/isg){
			my $size = $1;
			$size =~ s/^\s+|\s+$//isg;
			if ($size =~ /add/is){
				next;
			} else {
				push @sizeArray, $size;
			}
		}
	}
	if (!@sizeArray){
		return;
	}
	
	my $price = '';
	my $salePrice = '';
	if ($content =~ /productlisting.*?<br>([^<]*)<br>/is){
		$price = $1;
		$price =~ s/\(.*?\)//isg;
		$price =~ s/^\s+|\s+$//isg;
	} elsif ($content =~ /productlisting.*?<br>.*?old[_-]*price>([^<]*).*?sale[_-]*price([^<]*)/is){
		$price = $1;
		$salePrice = $2;
		$price =~ s/\(.*?\)//isg;
		$price =~ s/^\s+|\s+$//isg;
		$salePrice =~ s/\(.*?\)//isg;
		$salePrice =~ s/^\s+|\s+$//isg;
	}
	
	# Bulid Product
    my %productHash = (
    				'ProductId'   => $productId,
    				'Description' => $description,
			        'Size'        => \@sizeArray,
			        'ImageURL'    => $imageUrl,
			        'Price' 	  => $price,
					'SalePrice'   => $salePrice,
			        'Time'        => Date::Time::getGMT()
                  );
    return %productHash;
}



1;